package xuQiuGenGai;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * @Test: 需求更改
 * @author: ZBoHang
 * @time: 2021/9/17 9:04
 */
class Test {

    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        ArrayList<Apple> greenApples = new ArrayList<>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.getColor())) {
                greenApples.add(apple);
            }
        }
        return greenApples;
    }

    public static List<Apple> filterApple(List<Apple> list, AppleAttribute filter) {
        List<Apple> res = new ArrayList<>();
        for (Apple apple : list) {
            if (filter.test(apple)) {
                res.add(apple);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        List<Apple> list = new ArrayList<>();
        list.add(new Apple("green", 200));
        list.add(new Apple("red", 100));
        List<Apple> list1 = filterApple(list, new AppleAttribute() {
            @Override
            public boolean test(Apple apple) {
                return "red".equals(apple.getColor());
            }
        });
        list1.forEach(new Consumer<Apple>() {
            @Override
            public void accept(Apple apple) {
                System.out.println(apple);
            }
        });
    }
}
public class Apple{
    private String color;
    private int weight;

    public Apple(String color, int weight) {
        this.color = color;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public int getWeight() {
        return weight;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "color='" + color + '\'' +
                ", weight=" + weight +
                '}';
    }
}
